<?php

namespace App\Traits;

use Illuminate\Support\Facades\DB;

trait ResponseApi
{
    public function responseSuccess($data) {
        return response()->json([
            'code' => 200,
            'msg' => '操作成功',
            'data' => $data
        ]);
    }

    public function responseError($msg) {
        return response()->json([
            'code' => $msg,
            'msg' => config('errcode.code')[$msg],
            'data' => []
        ]);
    }

    public static function printSql()
    {
        // 在需要打印SQL的语句前添加监听事件。
        DB::listen(function($query) {
            $bindings = $query->bindings;
            $sql = $query->sql;
            foreach ($bindings as $replace){
                $value = is_numeric($replace) ? $replace : "'".$replace."'";
                $sql = preg_replace('/\?/', $value, $sql, 1);
            }
            dd($sql);
        });
    }
    /**
     * 动态菜单父子级结构
     *
     * Author :kayson
     */
    public static function getChild($result, $parent_id = 0)
    {
        $arr = [];
        foreach ($result as $item) {
            $newarr = [];

            if ($item['parent_id'] == $parent_id) {
                $newitem =  self::getChild($result, $item['id']);
                $newarr['is_menu'] = $item['is_menu'];
                $newarr['path'] =  $item['menu_route'];
                $newarr['name'] =  $item['menu_key'];
                if($item['component']){
                    $newarr['component'] = $item['component'];
                }
                if ($item['is_menu'] == 'Y'){
                    $hidden = false;
                }else{
                    $hidden = true;
                }
                $newarr['meta'] = [
                    'title' => $item['menu_name'],
                    'icon' => $item['menu_icon'],
                    'hidden' => $hidden,
                    'activeMenu' => $item['active_menu']
                ];

                if ($newitem)
                {
                    $newarr['children'] = $newitem;

                }
                $arr[] = $newarr;
            }
        }
        return $arr;
    }

    public static function convertTree($arr, $key = '')
    {
        $con = [];
        foreach ($arr as $k => $val) {
            if (is_array($val))
                $con = array_merge($con, self::convertTree($val, $k));
            elseif (!empty($key))
                $con[] = $val . ':' . $key;
        }
        return $con;
    }
    /**
     * 行政区划父子级结构
     *
     * Author :kayson
     */
    public static function subTree( $data, $pid = 1) {
        // 返回的结果
        $arr = [];
        foreach ($data as $val) {
            // 给定的PID是当前记录的上级ID
            if ($pid == $val['parent_id']) {
                // 递归
                $val['children'] = self::subTree($data,$val['id']);
                if(!$val['children']){
                    unset($val['children']);
                }
                $arr[] = $val;

            }
        }
        return $arr;
    }
}
